const sysMsgMW = require('express').Router()
sysMsgMW.route('/wx')
    .post((req, res, next) => {
        if (req.jsonData.MsgType[0] == 'event') {
            //console.log(req.jsonData)
            //在订阅时候 新建用户数据库
            if (req.jsonData.Event[0] == 'subscribe') {
                const mongoose = require('mongoose')
                const userSchema = require('../../model/userSchema')
                const User = mongoose.model('user', userSchema)

                // 防止重复保存
                User.findOne({ userID: req.jsonData.FromUserName[0] }).then(isExist => {
                    if (!isExist) {
                        const newUser = new User({
                            userID: req.jsonData.FromUserName[0]
                        })
                        newUser.save()
                    }
                })

            }
        }
        next()
    })

module.exports = sysMsgMW

